package com.inei.cie.capituloviii;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.inei.cie.Carga_Trabajo_Activity;
import com.inei.cie.R;
import com.inei.cie.capituloix.CapituloIX_Activity;
import com.inei.cie.dao.CapituloVIIIDao;
import com.inei.cie.entidades.P8;

public class CapituloVIII_Patio_Activity extends Activity {

	// Data
	public String Titulo, Tipo;
	public int Otro;
	public Bundle b;
	public String id_local;
	public int Nro_Pred;
	public String edificacion;
	public P8 obj;
	public int cod;
	public CapituloVIIIDao cap8dao;

	// Componentes
	public Spinner sp6F23, sp6F24, sp6F25, sp6F26, spUnidad1;
	public EditText et6A3entero, et6A3decimal;
	public EditText et6F2, et6F22, et6FObservaciones;
	public TextView tvOtroEdificacion;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.capitulo_viii_patio);

		IniciarComponentes();
	}

	public void IniciarComponentes() {
		tvOtroEdificacion = (TextView) findViewById(R.id.tvOtroEdificacion);

		spUnidad1 = (Spinner) findViewById(R.id.spUnidad1);
		sp6F23 = (Spinner) findViewById(R.id.sp6F23);
		sp6F24 = (Spinner) findViewById(R.id.sp6F24);
		sp6F25 = (Spinner) findViewById(R.id.sp6F25);
		sp6F26 = (Spinner) findViewById(R.id.sp6F26);

		et6A3entero = (EditText) findViewById(R.id.et6A3entero);
		et6A3decimal = (EditText) findViewById(R.id.et6A3decimal);
		et6A3decimal.setTextColor(Color.BLACK);
		et6F22 = (EditText) findViewById(R.id.et6F22);
		et6FObservaciones = (EditText) findViewById(R.id.et6FObservaciones);
		et6F2 = (EditText) findViewById(R.id.et6F2);
		et6F2.setEnabled(false);

		et6F22.setEnabled(false);

		cargarData();
	}

	public void cargarData() {
		b = getIntent().getExtras();
		id_local = b.getString("id_local");
		cod = b.getInt("Codigo");
		Nro_Pred = b.getInt("Nro_Pred");
		Otro = b.getInt("TipoOtro");

		if (Otro == 1) {
			Titulo = "Patios";
		} else if (Otro == 2) {
			Titulo = "Losas Deportivas";
		} else if (Otro == 3) {
			Titulo = "Cisternas - Tanques";
		} else if (Otro == 4) {
			Titulo = "Muros de contenci\u00f3n";
		}

		tvOtroEdificacion.setText("Local Nro.  "
				+ id_local
				+ " - Predio : "
				+ (getIntent().getExtras().getInt("TITULO_PRED") < 10 ? "0"
						: "") + getIntent().getExtras().getInt("TITULO_PRED")
				+ " - " + Titulo);
		String co = cod + "";
		et6F2.setText((co.length() == 1 ? "0" : "") + cod);

		cap8dao = new CapituloVIIIDao(getApplicationContext());
		obj = cap8dao.obtenerP8(id_local, Nro_Pred, cod, 1);

		spUnidad1.setSelection(obj.getP8_area_Tip());
		String[] area = (obj.getP8_area() + "").split("\\.");
		if (Integer.parseInt(area[0].length() == 0 ? "0" : area[0]) != 0) {
			et6A3entero.setText("" + area[0]);
		}
		et6A3decimal.setText("00");

		// String pr = obj.getNro_Pred() + "";
		String pr = getIntent().getExtras().getInt("TITULO_PRED") + "";
		// et6F22.setText((pr.length() == 1 ? "0" : "") + obj.getNro_Pred());
		et6F22.setText((pr.length() == 1 ? "0" : "")
				+ getIntent().getExtras().getInt("TITULO_PRED"));

		if (obj.getP8_ejecuto() != 0) {
			sp6F23.setSelection(obj.getP8_ejecuto());
		}
		if (obj.getP8_Est_E() != 0) {
			sp6F24.setSelection(obj.getP8_Est_E());
		}
		if (obj.getP8_Ant() != 0) {
			sp6F25.setSelection(obj.getP8_Ant());
		}
		if (obj.getP8_RecTec() != 0) {
			sp6F26.setSelection(obj.getP8_RecTec());
		}
		et6FObservaciones.setText(obj.getP8_Obs());
	}

	public void guardar() {
		obj.setId_local(id_local);
		obj.setP8_2("P - " + cod);
		obj.setP8_area_Tip(spUnidad1.getSelectedItemPosition());
		String area = (et6A3entero.getText().toString().length() == 0 ? "0"
				: et6A3entero.getText().toString())
				+ "."
				+ (et6A3decimal.getText().toString().length() == 0 ? "0"
						: et6A3decimal.getText().toString());
		obj.setP8_area(Float.parseFloat(area));
		obj.setP8_ejecuto(sp6F23.getSelectedItemPosition());
		obj.setP8_Est_E(sp6F24.getSelectedItemPosition());
		obj.setP8_Ant(sp6F25.getSelectedItemPosition());
		obj.setP8_RecTec(sp6F26.getSelectedItemPosition());
		obj.setP8_Obs(et6FObservaciones.getText().toString());
		obj.setSwCompleto(true);
		cap8dao.actualizarP8(obj, 1);

		Intent irMenu = new Intent(this,
				CapituloVIII_Listado_General_Activity.class);
		irMenu.putExtras(getIntent().getExtras());
		startActivity(irMenu);

		finish();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.menu_capituloviii, menu);
		menu.getItem(0).setTitle("PATIOS");
		menu.getItem(1).setTitle("TERMINAR");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub

		switch (item.getItemId()) {
		case R.id.mnSalir:
			AlertDialog.Builder dialogo1 = new AlertDialog.Builder(this);
			dialogo1.setTitle("Importante");
			dialogo1.setMessage("Deseas Salir?");
			dialogo1.setIcon(android.R.drawable.ic_dialog_alert);
			dialogo1.setCancelable(false);
			dialogo1.setPositiveButton("Si",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							System.exit(0);
						}
					});

			dialogo1.setNegativeButton("No",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							return;
						}
					});
			dialogo1.show();
			break;
		case R.id.mnMarco:
			Intent irMarco = new Intent(this, Carga_Trabajo_Activity.class);
			irMarco.putExtras(getIntent().getExtras());
			startActivity(irMarco);
			finish();
			break;
		case R.id.mnIncio:
			Intent marco = new Intent(this,
					CapituloVIII_Listado_General_Activity.class);
			marco.putExtras(getIntent().getExtras());
			startActivity(marco);
			finish();
			break;
		case R.id.mnSiguiente:
			String area = (et6A3entero.getText().toString().length() == 0 ? "0"
					: et6A3entero.getText().toString())
					+ "."
					+ (et6A3decimal.getText().toString().length() == 0 ? "0"
							: et6A3decimal.getText().toString());
			float p8_1 = Float.parseFloat(area);
			int p8_3 = sp6F23.getSelectedItemPosition();
			int p8_4 = sp6F24.getSelectedItemPosition();
			int p8_5 = sp6F25.getSelectedItemPosition();
			int p8_6 = sp6F26.getSelectedItemPosition();

			if (p8_1 != 0 && p8_3 != 0 && p8_4 != 0 && p8_5 != 0 && p8_6 != 0) {
				guardar();
			} else {
				Toast.makeText(this, "Debe responder todas las preguntas",
						Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.mnCalculadora:
			Intent i = new Intent();
			i.setClassName("com.android.calculator2",
					"com.android.calculator2.Calculator");
			startActivity(i);
			break;
		case R.id.mnFoto:
			Intent foto = new Intent(CapituloVIII_Patio_Activity.this,
					CapituloIX_Activity.class);
			// b.putBoolean("pregunta", true);
			foto.putExtras(getIntent().getExtras());
			foto.putExtra("pregunta", true);
			startActivity(foto);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		Intent irPredio = new Intent(this,
				CapituloVIII_Listado_General_Activity.class);
		irPredio.putExtras(getIntent().getExtras());
		startActivity(irPredio);
		finish();
	}
}
